package com.missilike.api.controller;


import com.missilike.biz.IMilAccountService;
import com.missilike.core.domain.im.IMCallbackRequestBody;
import com.missilike.core.domain.im.IMResponseBody;
import com.missilike.core.result.CommonResult;
import com.missilike.core.service.IIMService;
import com.missilike.core.service.impl.IMService;
import com.missilike.core.utils.ResultUtils;
import com.missilike.repository.entity.MilAccount;
import com.missilike.repository.entity.extend.MilAccountExtend;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author pengpengli
 */
@Slf4j
@RestController
@RequestMapping("/api/im")
public class IMController {

    private final IIMService imService;
    private final IMilAccountService milAccountService;

    @Autowired
    public IMController(IMService imService, IMilAccountService milAccountService) {
        this.imService = imService;
        this.milAccountService = milAccountService;
    }

    @ApiOperation(value = "getUserSign", notes = "获取IM签名")
    @RequestMapping(value = "/getUserSign", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getUserSign(@RequestParam Long userAccount) {
        log.info("Start to execute /im/getUserSign body request={}", userAccount);
        String userSign = imService.getUserSign(userAccount);
        log.info("Start to execute /im/getAUserSign responese={}", userSign);
        return ResultUtils.ok(userSign);
    }

    @ApiOperation(value = "getUsersByCondition", notes = "搜索用户,其中返回结果 friendRelatationType：0为无关系，1为好友关系，2为申请关系 flollowRelatationType：0为未关注，1为关注")
    @RequestMapping(value = "/getUsersByCondition", method = {RequestMethod.GET}, produces = "application/json")
    @ResponseBody
    public CommonResult getUserByCondition(@RequestParam(required = false)@ApiParam(value = "发起搜索的用户id") String id, @RequestParam(required = false) String keyWord) {
        List<MilAccountExtend> users = milAccountService.getUserByCondition(id,keyWord);
        return ResultUtils.ok(users);
    }


    @ApiOperation(value = "chatCallback", notes = "单聊回调接口")
    @RequestMapping(value = "/chatCallback", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public IMResponseBody chatCallback(@RequestParam("SdkAppid") String sdkAppid, @RequestParam("ClientIP") String clientIP,
                                       @RequestParam("OptPlatform") String optPlatform, @RequestBody IMCallbackRequestBody requestBody) {
        IMResponseBody responseBody = imService.handleChatCallback(sdkAppid, clientIP, optPlatform, requestBody);
        return responseBody;
    }
}
